﻿using System;
using System.Collections;
using System.Data;
using System.Windows.Forms;
using AMS.Inventory.PDA.Castle.DataAccess;

namespace AMS.Inventory.PDA
{
    public partial class FrmTypename : Form
    {
        public ListBox.ObjectCollection _arrayCheck;
        private DataTable _checkTypeDt;
        private Hashtable _hashtable;
        private SqliteDBServer _sqlDb;
        private string _strClassName, _strTypeName;
        public string[] strItem;

        public FrmTypename()
        {
            InitializeComponent();
        }

        public FrmTypename(DataTable checkTypeDt, string strClassName, string strTypeName, SqliteDBServer sqlDb)
        {
            InitializeComponent();
            _strClassName = strClassName;
            _strTypeName = strTypeName;
            _sqlDb = sqlDb;
            _checkTypeDt = checkTypeDt;
        }

        /// <summary>
        /// 获取选取属性的集合
        /// </summary>
        ///<
        private void setArrayCheckValue()
        {
            //if (lstCheckTypeCollection.Items.Count > 0)
            //{
            //    foreach (object s in lstCheckTypeCollection.Items)
            //    {
            //        if(_arrayCheck!=null)
            //        _arrayCheck.Add(s);
            //    }
            //} 
            _arrayCheck = lstCheckTypeCollection.Items;
        }

        private void FrmTypename_Load(object sender, EventArgs e)
        {
            _hashtable = clsLocalization.ReadContectResource(Name, "ZH");
            clsLocalization.getControlNames(this); 
            DataTable typedt;
            typedt = getTableByCon("");
            _arrayCheck = lstCheckTypeCollection.Items;
            getItemstoList(typedt, lstTypeCollection);

            getItemstoList(_checkTypeDt, lstCheckTypeCollection);
            if (lstCheckTypeCollection.Items.Count > 0)
            {
                foreach (object s in lstCheckTypeCollection.Items)
                {
                    if (lstTypeCollection.Items.Contains(s))
                    lstTypeCollection.Items.Remove(s);
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstCheckTypeCollection.SelectedItem != null)
            {
                object objTypeItem = lstCheckTypeCollection.SelectedItem.ToString();
                if (lstCheckTypeCollection.Items.Contains(objTypeItem))
                lstCheckTypeCollection.Items.Remove(objTypeItem);
                if (!lstTypeCollection.Items.Contains(objTypeItem))
                    lstTypeCollection.Items.Add(objTypeItem);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!chkSelectAll.Checked)
            {
                ;
                if (lstTypeCollection.SelectedItem != null)
                {
                    object objTypeItem = lstTypeCollection.SelectedItem.ToString();
                    if (lstTypeCollection.Items.Contains(objTypeItem))
                    lstTypeCollection.Items.Remove(objTypeItem);
                    if (!lstCheckTypeCollection.Items.Contains(objTypeItem))
                        lstCheckTypeCollection.Items.Add(objTypeItem);

                }
            }
            else
            {
                int count;
                count = lstTypeCollection.Items.Count;
                if ( count> 0)
                {
                    for (int i = 0; i < count; i++)
                    {
                        lstCheckTypeCollection.Items.Add(lstTypeCollection.Items[i]);
                        
                    }
                        lstTypeCollection.Items.Clear();
                }
            }
        }

        private void chkClearList_Click(object sender, EventArgs e)
        {
            if (chkClearList.Checked)
            {
                if (DialogResult.Yes ==
                    MessageBox.Show(clsLocalization.getFromContentNames(_hashtable, "IsClearType"),
                                    ModCommon.MagboxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button1))
                {
                    int count;
                    count = lstCheckTypeCollection.Items.Count;
                    for (int i = 0; i < count; i++)
                    {
                        lstTypeCollection.Items.Add(lstCheckTypeCollection.Items[i]);

                    }
                    lstCheckTypeCollection.Items.Clear();
                }
                else
                {
                    chkClearList.Checked = false;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (lstCheckTypeCollection.Items.Count > 0)
            {
                if (DialogResult.Yes ==
                    MessageBox.Show(clsLocalization.getFromContentNames(_hashtable, "IsClosePage"),
                                    ModCommon.MagboxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button1))
                {
                    setArrayCheckValue();
                }
            }
            Close();
        }

        private void btnSure_Click(object sender, EventArgs e)
        {
            //setArrayCheckValue();
            _arrayCheck = lstCheckTypeCollection.Items;
            Close();
        }

        private void lstTypeCollection_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnAdd_Click(sender, e);
        }

        private void lstCheckTypeCollection_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemove_Click(sender, e);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            DataTable searchDt;
            searchDt = getTableByCon(txtKeyval.Text.Trim());
            getItemstoList(searchDt, lstTypeCollection);
        }
        public string getSelectSql(string strClassName, string strTypeName)
        {
            string sql;
            sql = "SELECT distinct keyVal,keyVal2,keyVal3 FROM AmsInventory_BaseDataInfo ";
            if (string.IsNullOrEmpty(strClassName))
            {
                if (!string.IsNullOrEmpty(strTypeName))
                {
                    sql = sql + "where  TypeName='" + strTypeName + "'";
                }
                else
                {
                    sql = sql + " where ClassName='" + strClassName +
                      "' and TypeName='" + strTypeName + "'";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(strTypeName))
                {
                    sql = sql + " where ClassName='" + strClassName + "' and TypeName='" + strTypeName + "'";
                }
                else
                {
                    sql = sql + " where ClassName='" + strClassName + "'";
                }
            }
            return sql;
        }

        /// <summary>
        /// 查询出要写入Xml的数据
        /// </summary>
        /// <param name="containStr"></param>
        /// <returns></returns>
        private DataTable getTableByCon(string containStr)
        {
           
            DataTable typeDt; 
            string sql;
            sql = getSelectSql(_strClassName, _strTypeName);
            if (!string.IsNullOrEmpty(containStr))
            {
                sql = sql+ " and KeyVal like '%" + containStr + "%'";
            }
            //else
            //{
            //    sql = "SELECT distinct keyVal,keyVal2,keyVal3 FROM AmsInventory_BaseDataInfo where ClassName='" + _strClassName +
            //          "' and TypeName='" + _strTypeName + "'";
            //}
            try
            {
                typeDt = _sqlDb.GetDataTable(sql);
            }
            finally
            {
                _sqlDb.ConnectedDbClose();
            }
            
            return typeDt;
        }

        private void getItemstoList(DataTable itemDt, ListBox lstContorl) 
        {
            if (itemDt != null && itemDt.Rows.Count > 0)
            {
                foreach (DataRow s in itemDt.Rows)
                {
                    lstContorl.Items.Add(s[0].ToString());
                }
            }
        }

        private void txtKeyval_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                btnSearch_Click(sender, e);
            }
        }
    }
}